"""
@author: Shizuku
@file: 2022-11-19-practice-practice.py
@time: 2022/11/20 9:41
"""
import requests

def scstore():
    goods = {'F00001': {'name': '苹果', 'price': '1.2'},
             'F00002': {'name': '香蕉', 'price': '2.2'},
             'F00003': {'name': '梨子', 'price': '3.2'}}
    wd = "欢迎进入三创购物系统"
    print(wd.center(50, "*"))
    while 1:
        option = input('请选择选项：\n1、登录\n2、注册\n')
        with open('userInfo.txt', 'r+') as f:
            user = {}
            for i in f.readlines():
                user = dict(user, **eval(i))
        if option == "1":
            login(user)
            print("以下是商品列表：\n", goods)
            restart = True
            while restart:
                restart = False
                choice = input('请选择选项：\n1、加入购物车\n2、查看购物车\n3、结账\n')
                if choice == '1':
                    addtoshoppingcar(goods)
                    restart = True
                elif choice == '2':
                    shoppingcar()
                    restart = True
                elif choice == '3':
                    checkout(user)
                    restart = True
                elif choice == 'exit':
                    print("退出")

        elif option == "2":
            register(user)
        elif option == "exit":
            print("退出")
            break
        else:
            print("请输入正确的选项！")


def login(user):
    restart = True
    while restart:
        restart = False
        global username
        username = input("请输入用户名：")
        passwd = input("请输入密码：")
        if username in user.keys() and passwd == user[username]['passwd']:
            print("登陆成功\n")
        else:
            restart = True
            print("用户名或密码错误，请重新输入")


def register(user):
        restart = True
        while restart:
            restart = False
            rusername = input("请输入用户名：")
            rpasswd = input("请输入密码：")
            if rusername in user.keys():
                restart = True
                print("该用户名已被占用，换个名字试试吧")
            else:
                print("注册成功，新用户获得￥100")
                newuser = {rusername: {'passwd': rpasswd, 'money': 100}}
                with open('userInfo.txt', 'a') as f:
                    f.write(str(newuser) + '\n')


def addtoshoppingcar(goods):
    code = input("请输入商品编号：")
    if code in goods.keys():
        qty = input("请输入购买数量：")
        with open(username + "shoppingcar.txt", 'a',
                  encoding='utf-8') as f:
            buy = goods[code]['name']
            total = int(qty) * float(goods[code]['price'])
            total = round(total, 2)
            f.writelines(buy + ' ' + qty + ' ' + str(total) + '\n')
    else:
        print("不存在该商品")

def shoppingcar():
    with open(username + "shoppingcar.txt", 'r+',
              encoding='utf-8') as f:
        item = []
        for i in f.readlines():
            item.append(i.strip())
        print(item)

def checkout(user):
    with open(username + "shoppingcar.txt", 'r+',
              encoding='utf-8') as f:
        item = []
        expense = 0
        for i in f.readlines():
            item.append(i.strip())
        for i in list(item):
            expense = expense + float(i.split()[2])
        if expense > float(user[username]['money']):
            print("余额不足，请充值")
        else:
            money = float(user[username]['money']) - expense
            print("本此消费", expense, "元，剩余", money, "元，欢迎下次光临！")
            with open(username + "shoppingcar.txt", 'w',
                      encoding='utf-8') as f:
                f.write('')
            user[username]['money'] = money
            with open('userInfo.txt', 'w', encoding='utf-8') as f:
                f.write('')
            with open('userInfo.txt', 'a', encoding='utf-8') as f:
                for i in list(user.items()):
                    qwe = {}
                    qwe[i[0]] = i[1]
                    f.write(str(qwe) + '\n')


def picture():
    room = requests.get("https://www.sanchuangedu.cn/wgyc.png")
    wy = requests.get("https://www.sanchuangedu.cn/luogan.png")
    dy = requests.get("https://www.sanchuangedu.cn/xiayuzhen.jpg")

    with open("a.jpg", "wb") as fp:
        fp.write(room.content)

    with open("b.jpg", "wb") as fp:
        fp.write(wy.content)

    with open("c.jpg", "wb") as fp:
        fp.write(dy.content)